import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:yuan_hang_app/res/w_colors.dart';
import 'package:yuan_hang_app/utils/size_utils.dart';
import 'package:yuan_hang_app/widget/w_loading.dart';
import 'package:yuan_hang_app/widget/w_text.dart';

enum Status {
  LOADING,
  NONE,
  EMPTY,
  ERROR,
}

class BasePage extends StatelessWidget {
  final Status? status;
  final Widget child;
  final String? hint;
  final double minHeight;
  final double? height;
  final Widget? loadingWidget;
  final String? emptyHint;
  final Widget? emptyWidget;
  final String? errorHint;
  final Widget? errorWidget;
  final bool enableRefresh;
  final List<Widget> _childrens = [];
  final VoidCallback? onTap;

  BasePage(
      {required this.child,
      this.status,
      this.hint,
      this.minHeight = 0,
      this.height,
      this.loadingWidget,
      this.emptyHint,
      this.emptyWidget,
      this.errorHint,
      this.errorWidget,
      this.enableRefresh = false,
      this.onTap});

  @override
  Widget build(BuildContext context) {
    _childrens.clear();
    switch (status) {
      case Status.LOADING:
        _childrens.add(
          Container(
            constraints: BoxConstraints(minHeight: minHeight),
            alignment: Alignment.center,
            child: loadingWidget ??
                Column(mainAxisSize: MainAxisSize.min, children: [
                  WLoading(
                    width: 30.px,
                    height: 30.px,
                    loadingColor: WColors.primaryColor,
                  ),
                  WText(
                    (hint != null && hint != '') ? ('${hint!}\n') : '加载中...',
                    style: TextStyle(
                        fontSize: 15.px,
                        color: WColors.subTextColor,
                        height: 2),
                  )
                ]),
          ),
        );
        break;
      case Status.EMPTY:
        _childrens.add(
          Container(
            constraints: BoxConstraints(minHeight: minHeight ?? 0),
            alignment: Alignment.center,
            child: emptyWidget ??
                GestureDetector(
                  onTap: onTap,
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      Image.asset(
                        'images/no_data.png',
                        width: 150.px,
                      ),
                      Padding(
                        padding: EdgeInsets.only(top: 10.px),
                        child: WText(
                          (emptyHint != null && emptyHint != '')
                              ? ('${emptyHint!}\n')
                              : '暂无数据，点击刷新',
                          style: TextStyle(
                              fontSize: 14.px, color: WColors.subTextColor),
                        ),
                      )
                    ],
                  ),
                ),
          ),
        );
        if (onTap == null && enableRefresh) {
          // 这里需要添加listView
          _childrens.add(ListView());
        }
        break;
      case Status.ERROR:
        _childrens.add(
          Container(
            constraints: BoxConstraints(minHeight: minHeight ?? 0),
            alignment: Alignment.center,
            child: errorWidget ??
                GestureDetector(
                  onTap: onTap,
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      Image.asset(
                        'images/net_error.png',
                        width: 150.px,
                      ),
                      Padding(
                        padding: EdgeInsets.only(top: 10.px),
                        child: WText(
                          (errorHint != null && errorHint != '')
                              ? ('${errorHint!}\n')
                              : '服务或网络异常，点击刷新',
                          style: TextStyle(
                              fontSize: 14.px, color: WColors.subTextColor),
                        ),
                      )
                    ],
                  ),
                ),
          ),
        );
        if (onTap == null && enableRefresh) {
          // 这里需要添加listView
          _childrens.add(ListView());
        }
        break;
      default:
        _childrens.add(child
          // SizedBox(
          //   width: SizeUtils.screenW(),
          //   height: height,
          //   child: child,
          // ),
        );
        break;
    }

    return Stack(children: _childrens);
  }
}
